package advanced.practice._1007;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);

        int N=scanner.nextInt();
        Student.sortColum=scanner.nextInt();
        List<Student>students=new ArrayList<>();

        for(int i=0;i<N;i++){
            students.add(new Student(scanner.next(),scanner.next(),scanner.nextInt()));
        }

        Collections.sort(students);
        Iterator iterator = students.iterator();

        while(iterator.hasNext()) {
            Student stu = (Student)iterator.next();
            stu.output();
        }
    }
}

class Student implements Comparable<Student>{
    static int sortColum=0;
    String id;
    String name;
    int grade;

    public Student(String id,String name,int grade){
        this.id=id;
        this.name=name;
        this.grade=grade;
    }

    @Override
    public int compareTo(Student other) {
        if (sortColum == 1) {
            return this.id.compareTo(other.id);
        } else {
            int res;
            if (sortColum == 2) {
                res = name.compareTo(other.name);
            } else {
                res = grade - other.grade;
            }
            if (res == 0) {
                res = id.compareTo(other.id);
            }
            return res;
        }
    }

    public void output(){
        System.out.println(id+" "+name+" "+grade);
    }
}
